import falcon

from WebBackend.utils.jwt_tools import decode_jwt
import App_setting as s


async def login_require(req: falcon.Request, resp: falcon.Response, resource, params):
    # print("Before chuche.............")
    if s.security.server_admin_password:
        token = req.headers.get('nibiru')
        if not token:
            token = req.get_cookie_values('nibiru')
            if token:
                token = token[0]
            else:
                raise falcon.HTTPUnauthorized(title="认证信息(JWT)错误", description="未找到JWT信息")
        status, payload = decode_jwt(token=token)
        if status == 200:
            if payload['jwt_mix_key'] != s.security.jwt_mix_key:
                raise falcon.HTTPUnauthorized(
                    title='认证信息(JWT)错误',
                    description="MIX KEY 不一致；可能服务器已重启；请重新登录",
                )
        else:
            raise falcon.HTTPUnauthorized(title="认证信息(JWT)错误", description=payload)
        req.context['jwt'] = payload
    else:
        pass


async def logout(req, resp, resource, world):
    print("After chuche.............")
    pass
